package com.ziulife.controller;

import com.alibaba.fastjson2.JSON;
import com.ziulife.dao.*;
import com.ziulife.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Enumeration;
import java.util.List;


/**
 * Servlet implementation class WeiboController
 */
@WebServlet(urlPatterns = { "*.udo" })
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
//	private static final String TOPERSION = "496773331@qq.com";
    UserDao uDao = new UserDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=utf-8");
		/* 允许跨域的主机地址 */
		 response.setHeader("Access-Control-Allow-Origin", "*");
		/* 允许跨域的请求方法GET, POST, HEAD 等 */
		 response.setHeader("Access-Control-Allow-Methods", "*·");
		/* 重新预检验跨域的缓存时间 (s) */
		 response.setHeader("Access-Control-Max-Age", "3600");
		/* 允许跨域的请求头 */
		 response.setHeader("Access-Control-Allow-Headers", "*");
		/* 是否携带cookie */
		 response.setHeader("Access-Control-Allow-Credentials", "true");
		String mn = request.getServletPath();
		mn = mn.substring(1);
		mn = mn.substring(0, mn.length() - 4);
		try {
			Method method = this.getClass().getDeclaredMethod(mn, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ParseException {

		// 记录所有参数，帮助调试
		Enumeration<String> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			String paramValue = request.getParameter(paramName);
			System.out.println("参数名: " + paramName + ", 参数值: " + paramValue);
		}

		PrintWriter out = response.getWriter();
		String phone = request.getParameter("phone");
		if (phone!=null && !"".equals(phone)){
			phone = phone.trim();
			List<User> list = uDao.query(phone);
			//将数据库中存储的date类型数据按照指定格式存放
			for (User user : list) {
				System.out.println(list);
			}
			String jsonArray = JSON.toJSONString(list);
			out.print(jsonArray);

		}else{
			System.out.println("手机号为空："+phone);
		}


	}

	private void getLikeByPhone(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ParseException {

		PrintWriter out = response.getWriter();
		String phone = request.getParameter("phone");
		System.out.println("手机号-" + phone);
		User user = uDao.getLikeByPhone(phone);
		System.out.println(user);
		String jsonResponse = "{" +
				"\"data\":" + JSON.toJSONString(user) +
				"}";
		out.print(jsonResponse);
		out.flush();


	}




	
}
